<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Co_bank extends Model
{
    //银行的添加与编辑
    public function addeditbank($data)
    {
        $ckdata = notEmpty(['banks_id' => lang("bank"), 'bankname' => lang("account_opening_bank_branch"), 'account_name' => lang("account_name"), 'account_num' => lang("account")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        //没有公司的时候公司id是0
        if (!$data['co_id']) {
            $data['co_id'] = 0;
        }
        $infos = Db::name('company')->where('id', $data['co_id'])->value('status');
        if ($infos == 'LOCK') {
            return array('msg' => $data['name'] . lang("status_locked_can't_be_edit"), 'status' => 0);
        }
        //添加银行
        if ($data['act'] == 'bankadd') {
            if ($data['is_main'] == 1) {
                //将之前的主账号变成非主要
                $this->where('co_id', $data['co_id'])->setField('is_main', 0);
            }
            unset($data['id']);
            $data['create_id'] = $data['update_id'] = session('userid');
            $data['create_at'] = $data['update_at'] = time();
            $linkmanid = $this->strict(false)->insertGetId($data);//银行表
            if ($linkmanid) {
                return array('msg' => $data['account_name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['account_name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'bankedit') {
            if ($data['is_main'] == 1) {
                //将之前的主账号变成非主要
                $this->where('co_id', $data['co_id'])->setField('is_main', 0);
            }
            unset($data['act']);
            $data['update_id'] = session('userid');
            $data['update_at'] = time();
            $linkmanid = $this->where('id', $data['id'])->update($data);//公司属性表
            if ($linkmanid !== false) {
                return array('msg' => $data['account_name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['account_name'] . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //公司银行的删除
    public function delbank($data)
    {
        $bankname = getinfo($data['bankid'], 'co_bank')['account_name'];
        $retlink = $this->where('id', $data['bankid'])->delete();//银行表
        if ($retlink) {
            return array('msg' => $bankname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $bankname . lang("failed_to_delete"), 'status' => 0);
        }
    }

    //获取公司下的银行的信息
    public function getBanks($data)
    {
        $datares = $this->alias('a')->join('nt_banks b', 'a.banks_id=b.id', 'left')->field('a.id,a.bankname,a.account_num,a.is_main,b.name')->where('a.co_id', $data['company_id'])->select();
        foreach ($datares as $datareskey=>$dataresval){
            if ($dataresval['is_main']==1){
                $datares[$datareskey]['is_mains']=lang("main");
            }else{
                $datares[$datareskey]['is_mains']='';
            }
        }
        return $datares;
    }
}